package org.wicket.jbpm.task.panel;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.ajax.markup.html.IndicatingAjaxButton;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Check;
import org.apache.wicket.markup.html.form.CheckGroup;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.wicket.jbpm.WicketJBPMSession;
import org.wicket.jbpm.engine.exception.WicketJbpmException;
import org.wicket.jbpm.engine.spring.JbpmCoreEngine;
import org.wicket.jbpm.engine.utils.AddJbpmUsers;
import org.wicket.jbpm.model.UserGroup;
import org.wicket.jbpm.service.group.UserGroupService;
import org.wicket.jbpm.service.user.UserService;

/**
 * @author Emmanuel Nollase - emanux 
 * created: May 31, 2009 - 7:29:50 PM
 */
public class ReAssignPanel extends Panel
{

    private static final Log log = LogFactory.getLog(ReAssignPanel.class);

    @SpringBean
    private JbpmCoreEngine jbpmEngine;
    @SpringBean
    private UserGroupService groupService;
    @SpringBean
    private UserService userService;

    private String jbpmGrps;
    protected String comments;

    public ReAssignPanel(final String id, final String taskInstanceId)
    {
	super(id);

	final Form form = new Form("jbpmform");
	add(form);

	final TextField textArea = new TextField("jbpmComment",
		new PropertyModel(this, "comments"));
	form.add(textArea);

	final DropDownChoice jbpmGrp = new DropDownChoice("jbpmGrps",
		new PropertyModel(this, "jbpmGrps"), groupService
			.findByDscpForWorkflow());
	form.add(jbpmGrp);

	final WebMarkupContainer wmc = new WebMarkupContainer("wmc");
	form.add(wmc.setOutputMarkupId(true));

	final CheckGroup chkJbpmUsr = new CheckGroup("jbpmUsr",
		new ArrayList<String>());

	final ListView listJbpm = new ListView("jbpmUsrs", userService
		.findForJbpmUser())
	{
	    @Override
	    protected void populateItem(ListItem item)
	    {
		final String jbpmusers = item.getDefaultModelObjectAsString();

		item.add(new Check("usr", item.getModel()));
		item.add(new Label("jbpmuser", jbpmusers));
	    }
	};
	chkJbpmUsr.add(listJbpm);
	wmc.add(chkJbpmUsr);

	form.add(new IndicatingAjaxButton("reassign", form)
	{
	    // @SuppressWarnings("unchecked")
	    @Override
	    protected void onSubmit(AjaxRequestTarget target, Form form)
	    {
		final List<String> jbpmusers = new ArrayList<String>();
		final String specGrp = jbpmGrp.getDefaultModelObjectAsString();
		if (StringUtils.isNotEmpty(specGrp))
		{
		    final UserGroup grp = groupService.findByDscp(specGrp);
		    if (grp == null)
		    {
			throw new WicketJbpmException("group must not be null");
		    }
		    final List<String> users = userService.findForJbpmUser(grp
			    .getId());
		    AddJbpmUsers.addUserInList(users, jbpmusers);
		}

		final List<String> lv = (List<String>) chkJbpmUsr
			.getModelObject();
		if (!lv.isEmpty())
		{
		    AddJbpmUsers.addUserInList(lv, jbpmusers);
		}

		if (!jbpmusers.isEmpty())
		{
		    // we do not want re-assign to same user :)
		    final String usercd = WicketJBPMSession.get().getUser()
			    .getCd();
		    if (jbpmusers.contains(usercd))
		    {
			jbpmusers.remove(usercd);
		    }
		    log.info("re-assign to: " + jbpmusers);
		    final String[] actors = AddJbpmUsers
			    .getGroupUsers(jbpmusers.toArray());

		    // re-assign first the task
		    jbpmEngine.reassignTask(taskInstanceId, actors, textArea
			    .getDefaultModelObjectAsString());
		    // loanAppService.update(loanApp);
		} else
		{
		    error("Please select user/group for re-assignment");
		}
	    }
	});
    }

    public String getJbpmGrps()
    {
	return jbpmGrps;
    }

    public void setJbpmGrps(String jbpmGrps)
    {
	this.jbpmGrps = jbpmGrps;
    }

    public String getComments()
    {
	return comments;
    }

    public void setComments(String comments)
    {
	this.comments = comments;
    }
}
